<template>
  <div class="search">
    <div class="search-header">
      <div class="search-header-left">
        <div class="search-header-left-title">搜索结果</div>
        <div class="search-header-left-desc">共找到 {{ total }} 条结果</div>
      </div>

      <div class="search-header-right">
        <a-select
          v-model:value="queryParams.sorted"
          class="search-header-right-select"
          :options="sortOptions"
          @change="handleSortedChange"
        >
          <a-select-option
            v-for="item in sortOptions"
            :key="item.value"
            :value="item.value"
          >
            {{ item.label }}
          </a-select-option>
        </a-select>
      </div>
    </div>

    <div class="search-content">
      <div
        class="search-content-list"
        v-if="searchResult.length > 0"
      >
        <div
          class="search-content-list-item"
          v-for="item in searchResult"
        >
          <div class="search-content-container">
            <img
              :src="item.thumbnailUrl"
              alt="视频缩略图"
              @click="locateToVideo(item.id)"
            />

            <div class="search-container-info">
              <span class="play-count"
                ><PlayCircleOutlined /> {{ formatNumber(item.viewCount) }}</span
              >
            </div>
          </div>

          <a-button
            type="link"
            class="search-video-title"
            :title="item.title"
            @click="locateToVideo(item.id)"
          >
            <strong>{{ item.title }}</strong>
          </a-button>

          <a-button
            type="link"
            class="search-video-info"
            @click="locateToUserProfile(item.userId)"
          >
            <span
              class="search-video-author"
              :title="item.nickname"
            >
              {{ item.nickname }}
            </span>

            <span class="search-video-split"> - </span>

            <span class="search-video-date">
              {{ timeFormat(item.publishTime) }}
            </span>
          </a-button>
        </div>

        <Pagination
          style="margin-top: 20px"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          :total="total"
          @pagination="getSearchResult"
        />
      </div>

      <div v-else>
        <a-empty />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useRoute } from 'vue-router'
import dayjs from 'dayjs'
import type { CvVideoSearchReqVO } from '@/types/search'
import { CvVideoItemRespVO } from '@/types/global'
import { searchVideo } from '@/api/search'
import { PlayCircleOutlined } from '@ant-design/icons-vue'
import { sortOptions } from '@/utils/options'

const route = useRoute()

const queryParams = ref<CvVideoSearchReqVO>({
  keyword: '',
  sorted: 0,
  pageNo: 1,
  pageSize: 10
})

const searchResult = ref<CvVideoItemRespVO[]>([])
const total = ref(0)

onMounted(() => {
  const keyword = route.query.keyword as string
  if (keyword) {
    queryParams.value.keyword = keyword
    getSearchResult()
  } else {
    searchResult.value = []
    total.value = 0
  }
})

watch(
  () => route.query.keyword,
  newKeyword => {
    if (newKeyword) {
      queryParams.value.keyword = newKeyword
      getSearchResult()
    } else {
      searchResult.value = []
      total.value = 0
    }
  }
)

/**
 * 排序类型变更
 */
const handleSortedChange = (sorted: number) => {
  queryParams.value.sorted = sorted
  getSearchResult()
}

/**
 * 获取搜索结果
 */
const getSearchResult = async () => {
  const res = await searchVideo(queryParams.value)
  searchResult.value = res.list
  total.value = res.total
}

/**
 * 时间格式化
 */
const timeFormat = (time: string) => {
  if (time) {
    return dayjs(time).format('YYYY-MM-DD')
  }
  return ''
}

/**
 * 跳转到视频详情页
 */
const locateToVideo = (videoId: number) => {
  window.open(`/video?videoId=${videoId}`)
}

/**
 * 跳转到用户主页
 */
const locateToUserProfile = (userId: number) => {
  window.open(`/profile?userId=${userId}`)
}

/**
 * 播放量格式化
 */
const formatNumber = (num: number) => {
  if (!num) {
    return 0
  }

  if (num >= 1000 && num < 10000) {
    return (num / 1000).toFixed(1) + 'K'
  } else if (num >= 10000) {
    return (num / 10000).toFixed(1) + 'W'
  } else {
    return num.toString()
  }
}
</script>

<style scoped>
.search-header {
  display: flex;
  justify-content: space-between;
}

.search-header-left {
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.search-header-left-title {
  font-size: 24px;
  font-weight: 700;
  color: #333;
}

.search-header-left-desc {
  font-size: 14px;
  font-weight: 400;
  color: #999;
}

.search-header-right {
  display: flex;
  align-items: center;
}

.search-header-right-select {
  width: 120px;
}

.search-content-list {
  display: flex;
  flex-wrap: wrap;
}

.search-content-list-item {
  width: 18%;
  margin: 10px;
}

.search-content-list-item img {
  width: 100%;
  border-radius: 8px;
  height: 120px;
  cursor: pointer;
}

.search-content-container {
  position: relative;
  width: 100%;
  overflow: hidden;
  cursor: pointer;
}

.search-container-info {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: space-between;
  background: rgba(0, 0, 0, 0.6);
  color: white;
  font-size: 14px;
  font-weight: bold;
}

.play-count {
  padding: 5px;
}

.search-video-title {
  font-size: 15px;
  padding: 0;
  color: #000000;
}

.search-video-info {
  font-size: 14px;
  padding: 0;
  color: #666;
  display: flex;
  align-items: center;
}

.search-video-info:hover {
  cursor: pointer;
}

.search-video-split {
  margin: 0 5px;
}
</style>
